﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：LoginForm.cs
//文件功能：登录窗口
//
//创建标识：齐李平 || 2011-03-24
//
//修改标识：
//修改描述：
//**********************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Hmc.IM.Common;
using Hmc.IM.Common.Base;
using Hmc.IM.Client.Core;
using Hmc.IM.Client.Properties;
using System.Net;
using System.Net.Sockets;
using System.Text;


namespace Hmc.IM.Client
{
    /// <summary>
    /// 登录Form
    /// </summary>
    public partial class NotifyForm : Form
    {
        #region 移动窗体相关事件

        private const int WM_NCLBUTTONDOWN = 0xA1;
        private const int HT_CAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public NotifyForm()
        {
            InitializeComponent();
            

            this.BackColor = Color.Yellow;
            this.TransparencyKey = this.BackColor;
            this.panelTop.MouseDown += new MouseEventHandler(panelTop_MouseDown);
            this.panelTop.MouseMove += new MouseEventHandler(panelTop_MouseMove);
            this.panelTop.MouseUp += new MouseEventHandler(panelTop_MouseUp);
            this.lblTitle.MouseDown += new MouseEventHandler(panelTop_MouseDown);
            this.lblTitle.MouseMove += new MouseEventHandler(panelTop_MouseMove);
            this.lblTitle.MouseUp += new MouseEventHandler(panelTop_MouseUp);

            this.Left = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Width - this.Width;
            this.Top = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Size.Height - this.Height;
        }

        #region 窗体移动事件
        private bool isMouseDown = false;
        private Point FormLocation;     //form的location
        private Point mouseOffset;      //鼠标的按下位置

        void panelTop_MouseUp(object sender, MouseEventArgs e)
        {
            isMouseDown = false;
        }

        void panelTop_MouseMove(object sender, MouseEventArgs e)
        {
            int _x = 0;
            int _y = 0;
            if (isMouseDown)
            {
                Point pt = Control.MousePosition;
                _x = mouseOffset.X - pt.X;
                _y = mouseOffset.Y - pt.Y;

                this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);
            }
        }

        void panelTop_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //isMouseDown = true;
                //FormLocation = this.Location;
                //mouseOffset = Control.MousePosition;
                ReleaseCapture();
                SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
            }
        }
        #endregion

        #region PBCLOSE鼠标事件
        private void pbClose_MouseEnter(object sender, EventArgs e)
        {
            pbClose.Image = Resources.login_x_;
        }

        private void pbClose_MouseLeave(object sender, EventArgs e)
        {
            pbClose.Image = Resources.login_x;
        }
        #endregion

        #region 关闭事件
        private void pbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        

        #region 最小化
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pbMin_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        #endregion

        #region 窗体初始化事件
        /// <summary>
        /// 窗体加载事件（）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginForm_Load(object sender, EventArgs e)
        {
            #region 去除系统默认边框
            Rectangle clientRectangle = ClientRectangle;
            Point clientPoint = PointToScreen(new Point(0, 0));
            clientRectangle.Offset(clientPoint.X - Left, clientPoint.Y - Top);
            Region = new Region(clientRectangle);
            #endregion

            this.Text = CommonTools.SoftName + CommonTools.SoftVersion;
            lblTitle.Text = CommonTools.SoftName + CommonTools.SoftVersion;

            //lblLoginHint.Text = "";
        }

        #endregion

        /// <summary>
        /// 设置按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSetting_Click(object sender, EventArgs e)
        {
            LoginSettingForm frm = new LoginSettingForm();
            frm.ShowDialog();
        }



        /// <summary>
        /// 写入委托
        /// </summary>
        /// <param name="msg"></param>
        delegate void WriteLog(string msg);

        /// <summary>
        /// 写入登陆信息
        /// </summary>
        /// <param name="msg">输出信息</param>
        private void SetLoginText(string msg)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new WriteLog(SetLoginText), msg);
            }
            else
            {
                lblLoginHint.Text = msg;
            }
        }

    }
}
